#! /usr/bin/python

# Product Categories
#   jucarii
#   electronice
#   ingrijire_personala
#   carti
#   haine

from products import Product
from products import GiftAttribute
from constants import Constants
from constants import Utils
import re
import urllib2
from BeautifulSoup import BeautifulSoup
from database_connection import DBConnection

class ZavviParser:

    def __init__(self, b_url, cat, min_age, max_age, sex):
        self.base_url = b_url
        self.cat = cat
        "Get category id"
        conn = DBConnection("localhost", "root", "sac_recomandari", "sac_recomandari")
        self.cat_id = conn.get_cat_id(self.cat)
        self.attrs_dict = conn.get_all_attrs()
        self.pid = conn.get_last_prod_id() + 1
        conn.closeConn()
        self.min_age = min_age
        self.max_age = max_age
        self.sex = sex
        self.utils = Utils()

    def print_url(self):
        print self.base_url

    def parse_links(self):
        print "Parsing links...."
        conn = DBConnection("localhost", "root", "sac_recomandari", "sac_recomandari")
        for i in range(0, 1):
            file_to_read = urllib2.urlopen(self.base_url+"?pageNumber="+str(i)).read()
            soup = BeautifulSoup(file_to_read)
            trash = soup.findAll("p", attrs={"class":"product-name"})
            for el in trash:
                new_link = el.contents[1].attrs[0][1]
                new_prod, attr_list = self.download_content(new_link)

                "Now, let's add product to database..."
                if new_prod:
                    new_prod.printProduct("product_log")
                    conn.add_item_to_cadouri_table(new_prod)
                "... and attributes"
                if attr_list:
                    for atr in attr_list:
                        #atr.printAttribute("attr_log")
                        conn.add_item_to_gift_attrs_table(atr)

        conn.closeConn()

    def download_content(self, link):
        print "Link = %s" %link
        my_attrs = {}
        file_to_read = urllib2.urlopen(link).read()
        soup = BeautifulSoup(file_to_read)

        name = soup.find("div", attrs={"class":"product-title-wrap"})

        if not name:
            "ERROR Name = None"
            return None, None
        else:
            name = name.text
        name = name.encode("cp850", "ignore")
        name.encode("utf8", "ignore")
        name = name.replace('\'', '\\\'')

        review_no = re.search("\d+", soup.find("p", attrs={"class":"write-review"}).text).group()
        my_attrs[Constants.review_no] = review_no

        price = re.search('&#163;(\d+\.?\d*)', soup.find("p", attrs={"class":"product-price"}).text).group(1)
        my_attrs[Constants.price] = price

        details = soup.find("div", attrs={"id":"technicaldetails"})
        features = ""
        color = ""
        if details:
            details = details.findAll("tr")
            if details:
                if '/technology/' in link or '/electronics/' in link:
                    features = link.rsplit('/')[4]
                else:
                    features = ""
                for det in details:
                    aux = det.text.encode("utf8", "ignore")
                    aux = aux.rsplit(":")
                    key = aux[0]
                    values = aux[1]
                    if key == 'Colour':
                        color = values
                    elif key in ['Brand', 'Genre', 'Author', 'Artist']:
                        values = values.replace('\'', "\\\'")
                        features += "%s:%s, "%(key, values) 

        my_attrs[Constants.sex] = self.sex

        if color == "":
            my_attrs[Constants.color] = 'None'
        else:
            my_attrs[Constants.color] = color

        plink = soup.find("p", attrs={'class':'img-zoom-instructions'})
        regex = '[Hh][Rr][Ee][Ff]=\"(?P<link>(http://.*\.(jpg|jpeg|png)))'

        m = re.search(regex, str(plink))
        if m:
            plink = m.group('link')
        else:
            plink = "None"

        pdesc = soup.find('div', attrs={'itemprop':'description'})
        if pdesc:
            if pdesc.__class__ != 'uncode':
                pdesc.text.encode("utf8", "ignore")
            pdesc = pdesc.text.replace('\'', '\\\'')
            regex = "Age:(?P<age>(\d+))\+?"
            m = re.search(regex, pdesc)
            if m:
                my_attrs[Constants.min_age] = m.group('age')
            else:
                my_attrs[Constants.min_age] = self.min_age
        else:
            my_attrs[Constants.min_age] = self.min_age
            pdesc = ""
        my_attrs[Constants.max_age] = self.max_age 

        keywords = self.utils.get_no_of_keywords(pdesc, 5)
        my_attrs[Constants.keywords] = keywords

        new_product = Product(name, plink, pdesc, link, self.cat_id)

        offer = soup.find('div', attrs={'class':'product-promo'})
        if offer:
            my_attrs[Constants.gift_of_the_week] = "True"
        else:
            my_attrs[Constants.gift_of_the_week] = "False"

        if features == "":
            my_attrs[Constants.features]='None'
        else:
            my_attrs[Constants.features] = features

        attr_list = []
        for attr_key in self.attrs_dict:
            attr_id = self.attrs_dict[attr_key]
            attr_value = my_attrs[attr_key]
            attribute = GiftAttribute(attr_id, self.pid, attr_value)
            attr_list.append(attribute)

#        import pdb; pdb.set_trace()
        self.pid += 1
        return new_product, attr_list
